<template>
  <el-dialog
    title="设置音量"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="50%">
    <div>
      <el-form size="mini">
        <el-form-item label="默认音量">
          <el-input-number v-model="defaultVol"  :min="0" :max="100" label="音量"></el-input-number>
        </el-form-item>
        <!--<el-form-item label="音量计划">-->
          <!--<el-radio-group v-model="status">-->
            <!--<el-radio-button label="0">关闭</el-radio-button>-->
            <!--<el-radio-button label="1">开启</el-radio-button>-->
          <!--</el-radio-group>-->
        <!--</el-form-item>-->
        <el-form-item
          v-if="status == 1"
          v-for="(time, index) in times"
          :label="'时间段' + (index + 1)"
          :key="index"
          :rules="{ required: true, message: '时间段不能为空', trigger: 'blur'}">
          <el-time-picker
            :picker-options="{
                selectableRange: (index == 0?'00:00:00':times[index - 1].time[1].pattern('HH:mm:ss')) + ' - 23:59:59'
             }"
            v-model="times[index].time[0]"
            placeholder="开始时间">
          </el-time-picker>
          <el-time-picker
            v-model="times[index].time[1]"
            :picker-options="{
                 selectableRange:(times[index].time[0].pattern('HH:mm') + times[index].time[0].getSeconds() + 1) +  ' - 23:59:59'
            }"
            placeholder="结束时间">
          </el-time-picker>
          <el-input-number v-model="times[index].vol"  :min="0" :max="100" label="音量"></el-input-number>
          <el-button v-if="times.length > 1 || index > 0" type="warning" size="mini" @click.prevent="removeTime(time)">-</el-button>
          <el-button v-if="index + 1 == times.length" type="success" size="mini" @click.prevent="addTime">+</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submit()">确定</el-button>
    </span>
    </div>
  </el-dialog>
</template>
<style scoped>
  .el-dialog__body {
    padding: 10px !important;
  }

  .el-button.is-circle {
    padding: 7px !important;
  }
</style>
<script>
  export default {
    name: 'set-vol',
    data: function () {
      return {
        status: 0,
        defaultVol: 20,
        deviceIds: [],
        dataListLoading: false,
        dataListSelections: [],
        visible: false,
        times: [
          {time: [new Date(), new Date()], vol: 20}
        ]
      }
    },
    mounted () {
    },
    methods: {
      // 获取数据列表
      getInfo: function () {
        this.dataListLoading = true
        console.log(this.deviceIds)
        this.$http({
          url: this.$http.adornUrl('/sys/theme/by-device'),
          method: 'post',
          data: this.$http.adornData(this.deviceIds, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            console.log(data)
          }
          this.dataListLoading = false
        })
      },
      init: function (data) {
        this.visible = true
        this.deviceIds = data
        if (data.length === 1) {
          this.getInfo()
        }
      },
      submit () {
        var form = {}
        form.dids = this.deviceIds
        form.defaultVol = this.defaultVol
        if (this.status === '1') {
          form.vols = this.getVolSet()
        } else {
          form.vols = []
        }
        this.$http({
          url: this.$http.adornUrl('/generator/stb/set-vol'),
          method: 'post',
          data: this.$http.adornData(form)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      removeTime (item) {
        if (this.times < 2) {
          return
        }
        var index = this.times.indexOf(item)
        if (index !== -1) {
          this.times.splice(index, 1)
        }
      },
      addTime () {
        console.log(this.times)
        var lastTime = this.times[this.times.length - 1].time
        if (lastTime === null || lastTime.length < 2) {
          this.$message.error('上一个时间段未填写完整')
          return
        }
        this.times.push({
          time: [this.times[this.times.length - 1].time[1], new Date('1995/10/18 23:59:59')],
          vol: 20
        })
      },
      getVolSet () {
        var sets = []
        for (var i = 0; i < this.times.length; i++) {
          var time = this.times[i]
          var set = {}
          set.vol = time.vol
          set.startTime = time.time[0].pattern('yyyy-MM-dd HH:mm:ss')
          set.endTime = time.time[1].pattern('yyyy-MM-dd HH:mm:ss')
          sets.push(set)
        }
        return sets
      }
    }
  }
</script>
